package com.adou.d1;

import org.junit.Test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * Statment详细解说
 */
public class JDBCDemo3 {
    @Test
    public void testDML () throws Exception{
        // 1.注册驱动
        Class.forName("com.mysql.jdbc.Driver");

        String url = "jdbc:mysql://127.0.0.1/db1?useSSL=false";
        String username = "root";
        String password = "root";
        // 2.获取连接
        Connection conn = DriverManager.getConnection(url, username, password);

        // 3.sql
        String sql = "update account1 set money = 4000 where id = 1";

        // 4.获取执行sql对象
        Statement stmt = conn.createStatement();

        // 5.执行sql
        int result = stmt.executeUpdate(sql);// 执行完DML, 受影响行数
        // 6.打印结果
        System.out.println(result);

        if(result > 0) {
            System.out.println("修改成功");
        } else {
            System.out.println("修改失败");
        }

        // 7.释放资源
        stmt.close();
        conn.close();
    }

    @Test
    public void testDDL () throws Exception{
// 1.注册驱动
        Class.forName("com.mysql.jdbc.Driver");

        String url = "jdbc:mysql://127.0.0.1/db1?useSSL=false";
        String username = "root";
        String password = "root";
        // 2.获取连接
        Connection conn = DriverManager.getConnection(url, username, password);

        // 3.sql
        String sql = "create database db2";

        // 4.获取执行sql对象
        Statement stmt = conn.createStatement();

        // 5.执行sql
        int result = stmt.executeUpdate(sql);// 执行完DDL, 返回0 根据异常区分成功与否
        System.out.println(result);

        System.out.println("修改成功");

        // 7.释放资源
        stmt.close();
        conn.close();
    }


}
